#!/usr/bin/env python
# coding:utf-8

class NumConv:

    @staticmethod
    def mod_crc(buff):
        """对list进行crc编码

        获得CRC编码结果

        Args:
            buff:需要CRC编码内容

        Returns:
            int:CRC编码结果
            example:
                crc=mod_crc([0x00, 0xAB, 0xCD, 0x08 ... ])
                crc = 0xFB
                
        Raises:
            None
        """
        byte_crc = 0
        for i in range(0, len(buff)):
            byte_crc = byte_crc + buff[i]
        byte_crc = byte_crc % 256
        return byte_crc
    
    @staticmethod
    def mod_crc16_xmodem(buff):
        """对list进行crc16_xmodem编码

        获得CRC编码结果

        Args:
            buff:需要CRC编码内容

        Returns:
            list:CRC编码结果
            example:
                crc=mod_crc([0x00, 0xCC, 0xDD, 0x06, 0x00 ... ])
                crc = [0xF5, 0x71]
                
        Raises:
            None
        """
        crc = 0x0000
        poly = 0x1021
        byte = 0
        data_length = len(buff)
        for index in range(data_length):
            byte = buff[index]
            crc = crc ^ (byte << 8)
            for i in range(8):
                if (crc & 0x8000):
                    crc = (crc << 1) ^ poly
                else:
                    crc = crc << 1
        crc = crc & 0xFFFF
        #翻转crc:低位在前，高位在后
        crc_low_high = NumConv.get_hex_low_high_16(crc)
        return crc_low_high
    
    @staticmethod
    def orx_crc(buff):
        """对list进行crc编码

        获得CRC编码结果

        Args:
            buff:需要CRC编码内容

        Returns:
            int:CRC编码结果
            example:
                crc=orx_crc([0x00, 0xAB, 0xCD, 0x08 ... ])
                crc = 0xFB
                
        Raises:
            None
        """

        count = 0
        for i in range(0, len(buff)):
            if count>0:
                byte_crc = byte_crc ^ buff[i]
            else:
                byte_crc=buff[i]
            count+=1
        return byte_crc

    @staticmethod
    def get_hex_low_high_16(value):
        """计算--数值的16进制LIST，以低-高位排列
        Args:
            value:值

        Returns:
            int Array: [低位,高位]

        Raises:
            None
        """
        if value < 0:
            value = 65536 + value
        value_HI = int(value/256)
        value_LO = value % 256
        value_HEX = [value_LO,value_HI]
        return value_HEX
        
    @staticmethod
    def get_hex_low_high_24(value):
        """计算--数值的16进制LIST，以低-高位排列
        Args:
            value:值

        Returns:
            int Array: [低位,高位]

        Raises:
            None
        """
        if value < 0:
            value = 16777216 + value
        value_HI = int(value/65536)
        value_MODH = value % 65536
        value_MI = int(value_MODH/256)
        value_LO = value_MODH % 256
        value_HEX = [value_LO,value_MI,value_HI]
        return value_HEX

    @staticmethod
    def get_hex_low_high_32(value):
        """计算--数值的16进制LIST，以低-高位排列
        Args:
            value:值

        Returns:
            int Array: [低位,高位]

        Raises:
            None
        """
        if value < 0:
            value = 4294967296 + value
        value_T = int(value/16777216)
        value_MODT = value % 16777216
        value_HI = int(value_MODT/65536)
        value_MODH = value_MODT % 65536
        value_MI = int(value_MODH/256)
        value_LO = int(value_MODH % 256)
        value_HEX = [value_LO,value_MI,value_HI,value_T]
        return value_HEX
        
        
    @staticmethod
    def buffer2value_16(buff_1, buff_0):
        v = buff_1*256 + buff_0
        if not 0 <= v < 65536: 
            raise (ValueError, ("hex number outside 16 bit range") )
        if v >= 32768: 
            v = v - 65536 
        return v 
        
    @staticmethod
    def buffer2value_24(buff_2, buff_1, buff_0):
        v = buff_2*256*256 + buff_1*256 + buff_0
        if not 0 <= v < 16777216: 
            raise (ValueError, ("hex number outside 24 bit range") )
        if v >= 8388608: 
            v = v - 16777216 
        return v
        
    @staticmethod
    def buffer2value_32(buff_3, buff_2, buff_1, buff_0):
        v = buff_3*256*256*256 + buff_2*256*256 + buff_1*256 + buff_0
        if not 0 <= v < 4294967296: 
            raise (ValueError, ("hex number outside 32 bit range")) 
        if v >= 2147483648:
            v = v - 4294967296 
        return v

    @staticmethod
    def buffer2uint_16(buff_1, buff_0):
        v = buff_1*256 + buff_0
        if not 0 <= v < 65536: 
            raise (ValueError, ("hex number outside 16 bit range") )
        return v 
        
    @staticmethod
    def buffer2uint_24(buff_2, buff_1, buff_0):
        v = buff_2*256*256 + buff_1*256 + buff_0
        if not 0 <= v < 16777216: 
            raise (ValueError, ("hex number outside 24 bit range") )
        return v
        
    @staticmethod
    def buffer2uint_32(buff_3, buff_2, buff_1, buff_0):
        v = buff_3*256*256*256 + buff_2*256*256 + buff_1*256 + buff_0
        if not 0 <= v < 4294967296: 
            raise (ValueError, ("hex number outside 32 bit range")) 
        return v

